<?php

class User extends MY_Controller {

	function __construct()
	{
		parent::__construct();
		$this->page=$this->module_codes['Users'];
		$this->load->model('user_model' , 'USER_M');
		$this->data['staff_type'] = $this->get_type(true);
		$this->data['specialty'] = $this->get_specialty(true);
	}
	
	function index()
	{
		
            $this->check_page_permission('access');
		$result = $this->USER_M->get_user_data();
		
		//if $result['count'] = 0 then there is no data
		//else generate array for all types
		if($result['count'] == 0){
			$this->data['count'] = 0;
		}
		else{
			$this->data['count'] = $result['count'];
			$this->data['result'] = $result['info'];
		}
		
		$this->load->view('head');
		$this->load->view('user/main', $this->data);
		$this->load->view('footer');
	}
	
	function get_type($as_choices = false)
	{
		//get all types
		
            $this->check_page_permission('access');
		$this->load->model('staff_type_model' , 'STM');
			$result = $this->STM->get_type();
			if($as_choices){
				$arr = array();
				foreach($result['info'] as $row){
					$arr[$row['id'] ] = $row['description'];
				}
				//print_r($arr);
				return $arr;
			}
			//return $result['info'];
	}
	
	function get_specialty($as_choices = false)
	{
		
            $this->check_page_permission('access');
		//get all specialty
		$this->load->model('Specialty_model' , 'SPM');
			$result = $this->SPM->get_specialty();
			if($as_choices){
				$arr = array();
				foreach($result['info'] as $row){
					$arr[$row['id'] ] = $row['name'];
				}
				//print_r( $result);
				return $arr;
			}
			return $result['info'];
	}
	
	function new_user($id='')
	{

            $this->check_page_permission('add');
		$user_info	 = $id ? $this->USER_M->get_user_data($id) : array();
		$submit_data = $this->input->post('user_form');	
		$submit_data = !is_array($submit_data) ? array() : $submit_data;


		$this->load->view('head');
		$this->load->view('user/user_form' , array_merge(array_merge($this->data,$user_info)  , $submit_data) );
		$this->load->view('footer');
	}
	
	function new_user2()
	{
		$data = $this->input->post('user_form');
		$this->form_validation->set_message('integer', 'The %s field is required.');
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		
		$id =  $data['id'];
		if ($this->form_validation->run('user_new') == FALSE)
		{			
			return $this->new_user();
		}
		//Edit:
		if(isset($id) && $id!=''){
			
                 $this->check_page_permission('edit');
				if($this->USER_M->update_user_by_id($data, $id))
				redirect('user/' , 'refresh');
			}
		else{
			
                $this->check_page_permission('add');
				if($this->USER_M->insert_new_user($data))
					redirect('user/' , 'refresh');
			}
		
	}
	
	function delete_user($id)
	{
		
        $this->check_page_permission('delete'); // Not used
		if(isset($id) && $id!=''){
			if($this->USER_M->delete_user_by_id($id))
			redirect('user/' , 'refresh');	
		}
	}


}

/* End of file user.php */
/* Location: ./system/application/controllers/user.php */
